#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) {
        int c, r, o, a, k, count, key;
        c = r = o = a = k = count = key = 0;
        for (auto ch : croakOfFrogs)
        {
            if (ch == 'c') {
                c++, count++;
                if (c <= r || c <= o || c <= a || c <= k) return -1;
                if (key) key--, count--;
            }
            else if (ch == 'r') {
                r++;
                if (c < r || r <= o || r <= a || r <= k) return -1;
            }
            else if (ch == 'o') {
                o++;
                if (c < o || r < o || r <= a || r <= k) return -1;
            }
            else if (ch == 'a') {
                a++;
                if (c < a || r < a || o < a || c <= k) return -1;
            }
            else {
                k++, key++;
                if (c < k || r < k || o < k || a < k) return -1;
            }
        }
        if (c != r || c != o || c != a || c != k) return -1;
        return count;
    }
};

class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) {
        string s("croak");
        int n = s.size();
        vector<int> hash(n);
        unordered_map<char, int> hashsi(n);

        for (int i = 0; i < n; i++) hashsi[s[i]] = i;

        for (auto ch : croakOfFrogs)
        {
            if (ch == s[0])
            {
                if (hash[n - 1]) hash[n - 1]--;
                hash[0]++;
            }
            else
            {
                int i = hashsi[ch];
                if (hash[i - 1] == 0) return -1;
                hash[i - 1]--, hash[i]++;
            }
        }
        for (int i = 0; i < n - 1; i++) if (hash[i] != 0) return -1;
        return hash[n - 1];
    }
};